package com.liaoye.hellocustomview.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import androidx.annotation.NonNull;

public class ReactRefreshSurfaceView extends SurfaceView {

    private Paint mPaint;
    private Path mPath;
    private static final String TAG = ReactRefreshSurfaceView.class.getSimpleName();

    public ReactRefreshSurfaceView(Context context) {
        super(context);
        init();
    }

    public ReactRefreshSurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ReactRefreshSurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }


    private void init() {
        mPaint = new Paint();
        mPaint.setColor(Color.argb(0x1F, 0xFF, 0xFF, 0xFF));
        mPaint.setTextSize(60);
        mPath = new Path();
        getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(@NonNull SurfaceHolder holder) {
                Log.i(TAG, "surfaceCreated: ");
                drawText(holder);
            }

            @Override
            public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {

            }

            @Override
            public void surfaceDestroyed(@NonNull SurfaceHolder holder) {
                Log.i(TAG, "surfaceDestroyed: ");

            }
        });

    }


    private void drawText(SurfaceHolder holder) {
        new Thread(() -> {
            while (true) {
                Rect rect = new Rect(0, 0, 1, 1);
                // lockCanvas(Rect ty）： 用于获取指定区域的画布，画布以外的区域会保持与屏幕内容一致，画布以内的区域依然保持原画布内容
                Canvas canvas = holder.lockCanvas(rect);
                Rect canvasReact = canvas.getClipBounds();
                if (getWidth() == canvasReact.width() && getHeight() == canvasReact.height()) {
                    Log.i(TAG, "drawText: " + "unlockCanvasAndPost1");
                    canvas.drawColor(Color.BLACK);
                    holder.unlockCanvasAndPost(canvas);
                } else {
                    Log.i(TAG, "drawText: " + "unlockCanvasAndPost2");
                    holder.unlockCanvasAndPost(canvas);
                    break;
                }
            }

            for (int i = 0; i < 10; i++) {
                if (i == 0) {
                    Canvas canvas = holder.lockCanvas(new Rect(10, 10, 600, 600));
                    canvas.drawColor(Color.RED);
                    holder.unlockCanvasAndPost(canvas);
                }

                if (i == 1) {
                    Canvas canvas = holder.lockCanvas(new Rect(30, 30, 570, 570));
                    canvas.drawColor(Color.GREEN);
                    holder.unlockCanvasAndPost(canvas);
                }

                if (i == 2) {
                    Canvas canvas = holder.lockCanvas(new Rect(60, 60, 540, 540));
                    canvas.drawColor(Color.BLUE);
                    holder.unlockCanvasAndPost(canvas);
                }

                if (i == 3) {
                    Canvas canvas = holder.lockCanvas(new Rect(200, 200, 400, 400));
                    mPaint.setColor(Color.argb(0x3F, 0xFF, 0xFF, 0xFF));
                    canvas.drawCircle(300, 300, 100, mPaint);
                    holder.unlockCanvasAndPost(canvas);
                }

                if (i == 4) {
                    Canvas canvas = holder.lockCanvas(new Rect(250, 250, 350, 350));
                    mPaint.setColor(Color.RED);
                    canvas.drawText(i + "", 300, 300, mPaint);
                    holder.unlockCanvasAndPost(canvas);
                }

                try {
                    Thread.sleep(800);
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }).start();
    }
}
